package com.example.demo;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.io.File;

//@Service(更具体，概念更清晰)
@Component//（更通用，使用较少，因为概念模糊）
public class MailService {
    @Resource
    private JavaMailSender javaMailSender;

    public void sendMail(String from,String to,String cc,
                         String subject,String text){
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom(from);
        msg.setTo(to);
        msg.setCc(cc);
        msg.setSubject(subject);
        msg.setText(text);
        javaMailSender.send(msg);
    }
    //发送带附件的邮件
    public void sendMailWithAttache(String from, String to, String cc,
                                    String subject, String text, File file)
        throws Exception{

        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg,true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setCc(cc);
        helper.setSubject(subject);
        helper.setText(text);
        helper.addAttachment(file.getName(),file);//附件
        javaMailSender.send(msg);
    }
    //发送带附件的邮件
    public void sendMailWithImg(String from, String to,
                                    String subject, String text,
                                String[] srcPath,String[] rids)
            throws Exception{

        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg,true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text,true);//true表示以html内容显示邮件
        for (int i = 0; i < srcPath.length; i++) {
            FileSystemResource resource = new FileSystemResource(new File(srcPath[i]));
            //1图片的id，2图片文件资源
            helper.addInline(rids[i],resource);
        }
        javaMailSender.send(msg);
    }

    //
    public void sendMailWithFreemarker(String from, String to,
                                String subject, String text)
            throws Exception{

        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg,true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text,true);//true表示以html内容显示邮件

        javaMailSender.send(msg);
    }
    //
    public void sendMailWithThymeleaf(String from, String to,
                                       String subject, String text)
            throws Exception{

        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg,true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text,true);//true表示以html内容显示邮件

        javaMailSender.send(msg);
    }
}
